package org.jivesoftware;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.CharBuffer;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

import com.google.gson.Gson;

public class RestFulService {
    
	public static String getBbsAddress(){
		String url ="http://"+SettingsManager.getLocalPreferences().getServername()+":9090";
		Response resq =null;
		try {
			WebClient client = WebClient.create(url);
			resq = client.path("plugins/integration/service/Interaction/rs/filing/bds").type(MediaType.TEXT_PLAIN_TYPE).accept(MediaType.TEXT_PLAIN_TYPE).get();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			return "";
		}
		if(resq.getStatus()==404){
			return "";
		}
		InputStream a = (InputStream)resq.getEntity();
		InputStreamReader temp = new InputStreamReader(a);
		CharBuffer cb = CharBuffer.allocate(1000);
		try {
			temp.read(cb);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String temp2 = cb.flip().toString();
		String temp3 = temp2;  
		return  temp3;
		
	}
	
	 public  static boolean isUiaInfo(String servername){
		    String url ="http://"+servername+":9090";
			Response resq = null;
			try {
				WebClient client = WebClient.create(url);
				resq = client.path("plugins/integration/service/Interaction/rs/filing/getuia").type(MediaType.TEXT_PLAIN_TYPE).accept(MediaType.TEXT_PLAIN_TYPE).get();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				return false;
			}
			if(resq.getStatus()==404){
				return false;
			}
			InputStream a = (InputStream)resq.getEntity();
			InputStreamReader temp = new InputStreamReader(a);
			CharBuffer cb = CharBuffer.allocate(1000);
			try {
				temp.read(cb);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String temp2 = cb.flip().toString();
			String temp3 = temp2;
			if(temp3.equals("")){
				return false;
			}else{
			   Gson gson = new Gson();
			   UiaInfo uiainfo = gson.fromJson(temp3,UiaInfo.class);
			   if(uiainfo.getSTSWebSiteName().equals("") || uiainfo.getSTSWebSitePort().equals("") ||uiainfo.getWebSiteIdentity().equals("") || uiainfo.getSSOSiteIdentity().equals("") || uiainfo.getMD5key().equals("") || uiainfo.getISVkey().equals("")){
				   return false;
			   }
			   SettingsManager.getLocalPreferences().setSTSWebSiteName(uiainfo.getSTSWebSiteName());
			   SettingsManager.getLocalPreferences().setSTSWebSitePort(uiainfo.getSTSWebSitePort());
			   SettingsManager.getLocalPreferences().setWebSiteIdentity(uiainfo.getWebSiteIdentity());
			   SettingsManager.getLocalPreferences().setSSOSiteIdentity(uiainfo.getSSOSiteIdentity());
			   SettingsManager.getLocalPreferences().setMD5key(uiainfo.getMD5key());
			   SettingsManager.getLocalPreferences().setISVkey(uiainfo.getISVkey());
			   return true;
			}	
	 }
	 
	 public static void main(String[] args){
		boolean a =  isUiaInfo("10.0.23.89");
	 }
	
}
